package com.zhongzhi.serviceimpl;

import java.awt.List;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.dao.ComputersDao;
import com.zhongzhi.service.ComputersService;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;
import com.zhongzhi.util.Verify;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class ComputersServiceItem implements ComputersService{
	@Autowired
	private ComputersDao computersDao;
	
	//增
	@Override
	public Bo insert(Computers computers) {
		log.info("后端:计算机:新增:"+computers);
		if (Verify.stringIsNull(computers.getName())) {
			return new Bo(false, 400, "计算机名称不能为空", 0,null);
		}else if (Verify.stringIsNull(computers.getComputersip())) {
			return new Bo(false, 400, "计算机站号不能为空", 0,null);
		}else if (Verify.ipIsNotIp(computers.getComputersip())) {
			return new Bo(false, 400, "IP地址配置格式不正确", 0,null);
		}else {
			JsonResult insertServer = insertServer(computers);
			if (insertServer.getCode()==202) {
				log.error("后端:计算机:根据id删除失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:计算机:新增成功");
				return new Bo(false, 200, "存储数据成功", 1,0);
			}
		}
	}
	//改
	@Override
	public Bo update(Computers computers) {
		log.info("后端:计算机:修改:"+computers);
		if (computers.getName()==null || computers.getName().equals("")) {
			return new Bo(false, 400, "计算机名称不能为空", 0,null);
		}else if (computers.getComputers() ==null || computers.getComputers().equals("")) {
			return new Bo(false, 400, "计算机站号不能为空", 0,null);
		}else if (computers.getComputersip().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")==false) {
			return new Bo(false, 400, "IP地址配置格式不正确", 0,null);
		}else {
			JsonResult insertServer = updateServer(computers);
			if (insertServer.getCode()==202) {
				log.error("后端:计算机:修改失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:计算机:修改成功");
				return new Bo(false, 200, "存储数据成功", 1,0);
			}

		}
	}
	//删
	@Override
	public Bo delete(Integer id) {
		log.info("后端:计算机:根据id删除:"+id);
		if (id<=0) {
			return new Bo(false, 400, "id不正确", 1,null);
		}else {
			
			
			JsonResult insertServer = deleteServer(id);
			if (insertServer.getCode()==202) {
				log.error("后端:计算机:删除失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:计算机:删除成功");
				return new Bo(false, 200, "存储数据成功", 1,0);
			}
		}
	}
	//查
	@Override
	public Bo select(Integer pages, Integer line) {
		log.info("后端:计算机:分页查询:pages="+pages+",line="+line);
		if (pages==null) {
			pages=1;
		}
		if (line==null) {
			line=20;
		}
		int a = (pages-1)*line;

		try {
			ArrayList<Computers> list = computersDao.select(a,line);
			log.info("后端:计算机:分页查询:pages="+pages+",line="+line+"成功:"+list);
			return new Bo(true, 200, "查询计算机成功",list.size(),list);
		} catch (Exception e) {
			log.error("后端:计算机:分页查询失败:"+e);
			return new Bo(false, 500, "查询计算机失败", 1,e);
		}
	}
	//查询总数
	@Override
	public Bo selectSize() {
		log.info("后端:计算机:查询设备总数");
		try {
			int size = computersDao.selectSize();
			log.info("后端:计算机:查询设备总数成功:"+size);
			return new Bo(true, 200, "查询计算机数量成功",1,size);
		} catch (Exception e) {
			log.error("后端:计算机:查询设备总数失败:"+e);
			return new Bo(false, 500, "查询计算机数量失败", 1,e);
		}
	}
	//根据id查询
	@Override
	public Bo selectById(Integer id) {
		log.info("后端:计算机:根据id查询数据:"+id);
		//数据校验
		if (id<=0) {
			return new Bo(false, 400, "查询计算机id编号不能小于0", 0,null);
		}else {
			try {
				Computers eq = computersDao.selectById(id);
				log.info("后端:计算机:根据id查询数据:"+id+"成功:"+eq);
				return new Bo(true, 200, "id查询计算机数量成功",1,eq);
			} catch (Exception e) {
				log.error("后端:计算机:根据id查询数据失败:"+e);
				return new Bo(false, 500, "id查询计算机数量失败", 1,e);
			}
		}

	}
	//7:根据计算机站号查询
	@Override
	public Bo selectByComputers(String computers) {
		log.info("后端:计算机:根据计算机站号查询:"+computers);
		try {
			ArrayList<Computers> eq = computersDao.selectByComputers(computers);
			log.info("后端:计算机:根据计算机站号查询:"+computers+"成功:"+eq);
			return new Bo(true, 200, "根据计算机站号查询",1,eq);
		} catch (Exception e) {
			log.error("后端:计算机:根据计算机站号查询失败:"+e);
			return new Bo(false, 500, "根据计算机站号查询", 1,e);
		}
	}

	@Override
	public Bo webinsert(Computers computers) {
		log.info("后端:计算机:服务器新增:"+computers);
		try {
			int a = computersDao.insert(computers);
			log.info("后端:计算机:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:计算机:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}

	}
	@Override
	public Bo webupdate(Computers computers) {
		log.info("后端:计算机:服务器修改:"+computers);
		try {
			int a = computersDao.update(computers);
			log.info("后端:计算机:服务器修改成功:"+a);
			return new Bo(true, 200, "服务器修改成功",1,a);
		} catch (Exception e) {
			log.error("后端:计算机:服务器修改失败:"+e);
			return new Bo(false, 500, "服务器修改失败", 1,e);
		}
	}
	@Override
	public Bo webdelete(Integer id) {
		log.info("后端:计算机:服务器删除:"+id);
		try {
			int a = computersDao.delete(id);
			log.info("后端:计算机:服务器删除成功:"+a);
			return new Bo(true, 200, "服务器删除成功",1,a);
		} catch (Exception e) {
			log.error("后端:计算机:服务器删除失败:"+e);
			return new Bo(false, 500, "服务器删除失败", 1,e);
		}

	}
	@Override
	public Bo webselect() {
		log.info("后端:计算机:服务器查询:");
		try {
			ArrayList<Computers> list =computersDao.webselect();
			log.info("后端:计算机:服务器查询成功:"+list);
			return new Bo(true, 200, "服务器查询成功",1,list);
		} catch (Exception e) {
			log.error("后端:计算机:服务器查询失败:"+e);
			return new Bo(false, 500, "服务器查询失败", 1,e);
		}

	}



	private JsonResult insertServer(Computers computers) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/computers/insert?id="+computers.getId()
		+"&name="+computers.getName()
		+"&computers="+computers.getComputers()
		+"&computersip="+computers.getComputersip()
		+"&remarks="+computers.getRemarks();
		try {
			log.info("后端:计算机:同步增加:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:计算机:同步增加结果:"+gethttp);
			return ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = computersDao.insert(computers);
				log.info("后端:计算机:同步增加失败,增加本地:"+insert);
				return new JsonResult(201, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:计算机:本地增加失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}
	}

	private JsonResult updateServer(Computers computers) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/computers/update?id="+computers.getId()
		+"&name="+computers.getName()
		+"&computers="+computers.getComputers()
		+"&computersip="+computers.getComputersip()
		+"&remarks="+computers.getRemarks();
		System.out.println(computers);
		try {
			log.info("后端:计算机:同步修改:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:计算机:同步修改结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int update = computersDao.update(computers);
				log.info("后端:计算机:同步修改失败,修改本地:"+update);
				return new JsonResult(201, "", 1, update);
			} catch (Exception e2) {
				log.error("后端:计算机:本地修改失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}
	}

	private JsonResult deleteServer(Integer id) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/computers/delete?id="+id;
		try {
			log.info("后端:计算机:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:计算机:同步删除结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int delete = computersDao.delete(id);
				log.info("后端:计算机:同步删除失败,删除本地:"+delete);
				return new JsonResult(201, "", 1, delete);
			} catch (Exception e2) {
				log.error("后端:计算机:本地删除失败:"+e2);
				return new JsonResult(202, "", 1, e2);
			}
		}
	}



}
